import {dbHelper} from "@/middleware/db/dbService";
import _ from 'lodash';

export let UserService = {

    addCurrentUserLogin(loginUser) {
        dbHelper.getDbHelper().currentUser.clear();
        dbHelper.getDbHelper().currentUser.put(loginUser);
    },

    async getCurrentUser() {
        const collection = await dbHelper.getDbHelper().currentUser.toCollection();
        console.log('peter get current user' + collection);
        return collection.first();
    },

    async updateUserLoginState(userLoginState) {
        dbHelper.getDbHelper().userLoginState.clear();
        dbHelper.getDbHelper().userLoginState.put(userLoginState);
        console.log('peter user login state' + userLoginState);
    },

    async getUserLoginState() {
        const collection = await dbHelper.getDbHelper().userLoginState.toCollection();

        if (_.isUndefined(collection.first())) {
            return {
                username: '',
                password: '',
                isSavePassword: true,
                isAutoLogin: true
            };
        }
        console.log('peter 123', collection.first().then(function (response) {
            console.log('peter first ' + response);
        }))
        return collection.first();
    },
}
